<?php

namespace App\Console\Commands;

use App\Model\UserCommissionDetail;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class Sumreward extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sumreward';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     */
    public function handle()
    {
        $users = User::all();
        foreach ($users as $user) {
            $id = $user->id;
            $m1 = UserCommissionDetail::where('relation_id', $id)->sum('money');
            $m2 = DB::table('commission_log')->where('user_id', $id)->where('title', 'like', '%活动%')->sum('amount');
            if (($m1 + $m2) > 0) {
                echo $m1 + $m2;
                $user->rewards_all = $m1 + $m2;
                $user->save();
            }
        }
    }
}
